<?php
namespace Plugin\Controller;
use Think\Controller;
use Think\Model;
/**
* 融云要用到的接口
*/
class RongcloudController extends Controller{

    public function index(){
    	$uid = $_SESSION['loginUser']['uid'];
		if($uid){
			$sql = "SELECT A.`uid`,MAX(A.`send_time`) AS 'send_time',B.`username`,B.`avater`,C.`title` FROM mini_chat_content A JOIN mini_users B ON A.`uid`=B.`uid` JOIN mini_schools C ON B.`uid`=C.`sid` WHERE A.`touid`=".$uid." GROUP BY A.`uid` ORDER BY send_time DESC";
			$model = new Model();
			$info = $model->query($sql);
			if($info){
				$this->users = $info;
			}
			
		}
        $this->display();
    }
    public function index_copy(){
    	$uid = $_SESSION['loginUser']['uid'];
		if($uid){
			$sql = "SELECT A.`uid`,MAX(A.`send_time`) AS 'send_time',B.`username`,B.`avater`,C.`title` FROM mini_chat_content A JOIN mini_users B ON A.`uid`=B.`uid` JOIN mini_schools C ON B.`uid`=C.`sid` WHERE A.`touid`=".$uid." GROUP BY A.`uid` ORDER BY send_time DESC";
			$model = new Model();
			$info = $model->query($sql);
			if($info){
				$this->users = $info;
			}
			
		}
        $this->display();
    }

    public function get_token(){
        if(!$_SESSION['loginUser']){
            _apiReturn(0,"对不起，当前用户尚未登录");
        }
        $config = C('RongCloud');
        Vendor('RongCloud.rongcloud');
        $RongCloud = new \RongCloud($config['ak'],$config['sk']);

        

        // 获取 Token 方法
        $data['ak'] = $config['ak'];
        $result = $RongCloud->user()->getToken($_SESSION['loginUser']['uid'], $_SESSION['loginUser']['username'], $_SESSION['loginUser']['avater'] ? $_SESSION['loginUser']['avater'] : 'http://'.$_SERVER['HTTP_HOST'].'/Public/res/images/avatar/default.png');
        $result = json_decode($result,true);
        //dump($result['token']);
        $data['token'] = $result['token'];
        _apiReturn(1,'success',$data);
    }
}